package com.icinfo.cloud.provider.punish.ucase.caseregister.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.csource.manage.dto.CaseSourceDetailDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseTemporaryRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.*;
import com.icinfo.cloud.provider.punish.ucase.caseregister.service.ICaseRegisterService;
import com.icinfo.cloud.provider.punish.ucase.caseregister.vo.*;
import com.icinfo.framework.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 【V1.3.0】描述:立案登记对应的Controller类.
 * @author lxj
 * @date 2022/2/24
 **/

@RestController
@RequestMapping("/punish/ucase/case/register")
@Slf4j
public class CaseRegisterController {

    @Resource
    private ICaseRegisterService caseRegisterService;
    @Resource
    ICaseDocumentRecordService caseDocumentRecordService;
    @Resource
    private CaseTemporaryRecordMapper caseTemporaryRecordMapper;

    /**
     *
     *
     * @param saveDto 保存dto
     * @return {@link Result<Map<String, Object>> }
     * @author shishengyao
     * @date 2022/05/28
     */
    @PostMapping(value = "/casePreview")
    public Result<Map<String, Object>> casePreview(@RequestBody @Validated CaseRegisterSaveDto saveDto) {
        Map<String, Object> map = caseRegisterService.getCasePreview(saveDto);
        return Result.success(map);
    }

    /**
     * 案件登记回显数据查询
     *
     * @param caseId
     * @return
     * @author lxj
     * @date 2022/3/6
     **/
//    @RequestMapping(value = "/getCaseSaveMsg")
//    public Result<JSONObject> getCaseSaveMsg(@RequestBody String caseId) {
//        try {
//            if (StringUtils.isEmpty(caseId)) {
//                return Result.error("caseId 未传递");
//            }
//            JSONObject obj = JSON.parseObject(caseId);
//            CaseDocumentRecord vo = caseDocumentRecordService.getCaseDocumentRecord(obj.getString("caseId"), Constant.CASE_REG_CODE);
//            if (vo == null) {
//                //查询暂存表是否有数据
//                Example example = new Example(CaseTemporaryRecord.class);
//                example.createCriteria().andEqualTo("caseId", obj.getString("caseId"))
//                        .andEqualTo("documentCatalogCode", Constant.CASE_REG_CODE);
//                List<CaseTemporaryRecord> list = caseTemporaryRecordMapper.selectByExample(example);
//                if (list != null && list.size() > 0) {
//                    return Result.success("", JSONObject.parseObject(list.get(0).getContent()));
//                }
//                return Result.error(Constant.STRING_NOT_FOUND);
//            }
//            JSONObject jsonObject = JSONObject.parseObject(vo.getDocumentContent());
//            return Result.success(jsonObject);
//        } catch (Exception e) {
//            return Result.error(e.getMessage());
//        }
//    }

    /**
     * 案件登记删除
     *
     * @param caseId
     * @return
     * @author lxj
     * @date 2022/3/6
     **/
    @RequestMapping(value = "/delCaseSaveMsg")
    public Result<JSONObject> delCaseSaveMsg(@RequestBody String caseId) {
        if (StringUtils.isEmpty(caseId)) {
            return Result.error("caseId 未传递");
        }
        JSONObject obj = JSON.parseObject(caseId);
        boolean flag = caseRegisterService.delCase(obj.getString("caseId"),"");
        if (flag) {
            return Result.success(Constant.STRING_SUCCESS_EN);
        }
        return Result.error("失败");
    }

    /**
     * 获取有执法证协办人员列表
     *
     * @return
     * @author lxj
     * @date 2022/3/6
     **/
    @PostMapping(value = "/getAssistHandleList")
    public Result getAssistHandleList() {
        //是否包含登录人 1 包含 0 不包含
        String isContainLogin=Constant.STRING_0;
        List<HandleAssistVo> list = caseRegisterService.getAssistHandleList(isContainLogin);
        return Result.success(Constant.STRING_SUCCESS_EN, list);
    }
    /**
     * 获取有执法证协办人员列表（包含登录人）
     * @author liyafeng
     * @date 2022/10/21
      * @param
     * @return {@link Result}
     */
    @PostMapping(value = "/getAssistHandleListContainLogin")
    public Result getAssistHandleListContainLogin() {
        //是否包含登录人 1 包含 0 不包含
        String isContainLogin=Constant.STRING_1;
        List<HandleAssistVo> list = caseRegisterService.getAssistHandleList(isContainLogin);
        return Result.success(Constant.STRING_SUCCESS_EN, list);
    }

    /**
     * 根据类型获取有执法证协办人员列表（1-机构 2-部门）
     * @param type
     * @return
     */
    @GetMapping(value = "/getAssistHandleListByType")
    public Result getAssistHandleListByType(@RequestParam("type") String type) {
        List<HandleAssistVo> list = caseRegisterService.getAssistHandleListByType(type);
        return Result.success(Constant.STRING_SUCCESS_EN, list);
    }

    /**
     *
     * 获取下级机构信息
     * @return
     * @author lxj
     * @date 2022/3/6
     **/
    @GetMapping(value = "/getOrgListByTopOrg")
    public Result getOrgListByTopOrg(@RequestParam("orgCode") String orgCode) {
        List<CaseHandoutVo> list = caseRegisterService.getOrgListByTopOrg(orgCode);
        return Result.success(Constant.STRING_SUCCESS_EN, list);
    }

    /**
     * 【V2.0.0】描述：立案登记（多案由、多当事人）
     *
     * @return {@link Result }
     * @author shishengyao
     * @date 2022/05/10
     */
    @PostMapping(value = "/saveCaseRegister")
    public Result<CaseRegisterSaveVo> saveCaseRegister(@RequestBody @Validated CaseRegisterSaveDto saveDto) {
        CaseRegisterSaveVo caseRegisterSaveVo = caseRegisterService.saveCaseRegister(saveDto);
        if (ObjectUtil.isNotEmpty(caseRegisterSaveVo)) {
            return Result.success(caseRegisterSaveVo);
        }
        return Result.error("立案失败！");
    }

    /**
     * 【V2.0.0】描述：暂存立案登记（多案由、多当事人）
     *
     * @return {@link Result }
     * @author shishengyao
     * @date 2022/05/10
     */
    @PostMapping(value = "/saveCaseRegisterTemporary")
    public Result<CaseRegisterSaveVo> saveCaseRegisterTemporary(@RequestBody CaseRegisterSaveDto saveDto) {
        int count = caseRegisterService.saveCaseRegisterTemporary(saveDto);
        if (count>0) {
            return Result.success("暂存成功");
        }
        return Result.error("立案失败！");
    }

    /**
     * 【V2.0.0】描述：获取立案登记详情（多案由、多当事人）
     *
     * @param queryDto 查询dto
     * @return {@link Result<JSONObject> }
     * @author shishengyao
     * @date 2022/05/12
     */
    @PostMapping(value = "/getCaseRegisterDetail")
    public Result<CaseRegisterInfoVo> getCaseRegisterDetail(@RequestBody @Validated CaseRegisterDetailQueryDto queryDto) {
        try {
            CaseRegisterInfoVo caseRegisterInfo = caseRegisterService.getCaseRegisterInfo(queryDto);

            return Result.success(caseRegisterInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 【V1.3.1】描述：获取立案登记详重复立案列表
     *
     * @param pageRequest 页面请求
     * @return {@link Result<PageResponse< RepeatCaseInfoVo >> }
     * @author shishengyao
     * @date 2022/07/11
     */
    @PostMapping(value = "/getRepeatCaseInfoList")
    public Result<PageResponse<RepeatCaseInfoVo>> getRepeatCaseInfoList(@RequestBody @Validated RepeatCaseInfoPageRequest pageRequest) {
        try {
            List<RepeatCaseInfoVo>  voList= caseRegisterService.getRepeatCaseInfoList(pageRequest);
            return Result.success(new PageResponse<>(voList));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 【V1.3.2】描述： 根据案由编码及当事人获取N年内重复违法次数
     *
     * @author: zhengqiang
     * @date: 2022/7/26
     * @param: request
     * @return {@link Result< List< CaseMainReasonRepeatCountVo>>}
     **/
    @PostMapping(value = "/getCaseRepeatCountByReasonAndParty")
    public Result<List<CaseMainReasonRepeatCountVo>> getCaseRepeatCountByReasonAndParty(@RequestBody @Validated CaseRepeatRequest request) {
        List<CaseMainReasonRepeatCountVo> countList = caseRegisterService.getCaseRepeatCountByReasonAndParty(request);
        return Result.success(countList);
    }

    /**
     * 【V1.3.2】描述： 根据案由编码及当事人获取N年内重复违法列表
     * 【V1.10.0】描述： 根据案由编码及当事人获取N年内违法列表 *案由列表不用传
     *
     * @author: zhengqiang
     * @date: 2022/7/26
     * @param: pageRequest
     * @return {@link Result< PageResponse< CaseMainReasonRepeatVo>>}
     **/
    @PostMapping(value = "/getCaseRepeatListByReasonAndParty")
    public Result<PageResponse<CaseMainReasonRepeatVo>> getCaseRepeatListByReasonAndParty(@RequestBody @Validated CaseMainReasonRepeatPageRequest pageRequest) {
        List<CaseMainReasonRepeatVo> voList = caseRegisterService.getCaseRepeatListByReasonAndParty(pageRequest);
        return Result.success(new PageResponse<>(voList));
    }

    /**
     * 【V1.3.2】描述： 根据案由编码及当事人列表获取重复违法列表(分页)
     *
     * @author: zhengqiang
     * @date: 2022/8/4
     * @param: pageRequest
     * @return {@link Result< PageResponse< CaseMainReasonRepeatVo>>}
     **/
    @PostMapping(value = "/getCaseRepeatPageList")
    public Result<PageResponse<CaseMainReasonRepeatVo>> getCaseRepeatPageList(@RequestBody @Validated CaseRepeatPageRequest pageRequest) {
        List<CaseMainReasonRepeatVo> voList = caseRegisterService.getCaseRepeatPageList(pageRequest);
        return Result.success(new PageResponse<>(voList));
    }

    /**
     * 【V1.3.2】描述： 根据名称搜索机构用户
     *
     * @author: zhengqiang
     * @date: 2022/8/2
     * @param: userName
     * @return {@link Result}
     **/
    @GetMapping(value = "/searchOrgUserListByName")
    public Result<List<HandleAssistVo>> searchOrgUserListByName(@RequestParam String userName) {
        List<HandleAssistVo> list = caseRegisterService.searchOrgUserListByName(userName);
        return Result.success(Constant.STRING_SUCCESS_EN, list);
    }
    /**
     * 案源不予立案案件保存
     * @author liyafeng
     * @date 2022/8/17
      * @param caseSourceDetailDto
     * @return {@link Result< String>}
     */
    @PostMapping(value = "/saveNoCaseRegister")
    public Result<String> saveNoCaseRegister(@RequestBody @Validated CaseSourceDetailDto caseSourceDetailDto) {
        int num=caseRegisterService.saveNoCaseRegister(caseSourceDetailDto);
        if(num>0){
            return Result.success("不予立案保存成功");
        }
        return Result.error("不予立案保存失败");
    }

    /**
     * 【V1.4.1】描述： 外部接入案源处置回传结果
     *
     * @author: zhengqiang
     * @date: 2022/8/19
     * @param: caseId
     * @return {@link Result}
     **/
    @GetMapping("/sendDisposalBackResult")
    public Result sendDisposalBackResult(@RequestParam("caseId") String caseId) {
        return caseRegisterService.sendDisposalBackResult(caseId);
    }

}
